package com.bicon.bdata_service_search.es.bean;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MultiMatchQuery;

import java.io.Serializable;
import java.util.List;

/**
 * 描述:
 *
 * @author zhaoyu
 * @email zhaoyu@vv.cc
 * @create 2018-09-10 11:41
 */
public class QueryCondition implements Serializable {
    private static final long serialVersionUID = -8314198299201893177L;

    private QueryTypeEnum queryType;//查询类型

    private String conColumn;//列 名

    private String conValue;//列 值

    private String min;//下界（用于范围查询）

    private String max;//上界（用于范围查询）

    public QueryTypeEnum getQueryType() {
        return queryType;
    }

    public void setQueryType(QueryTypeEnum queryType) {
        this.queryType = queryType;
    }

    public String getConColumn() {
        return conColumn;
    }

    public void setConColumn(String conColumn) {
        this.conColumn = conColumn;
    }

    public String getConValue() {
        return conValue;
    }

    public void setConValue(String conValue) {

        this.conValue = conValue;

    }

    public String getMin() {
        return min;
    }

    public void setMin(String min) {
        this.min = min;
    }

    public String getMax() {

        return max;

    }

    public void setMax(String max) {

        this.max = max;

    }

    /**
     * 准确查询
     * 构造 列 conColumn 值为 conValue  查询条件
     *
     * @param conColumn
     * @param conValue
     * @return
     */
    public static QueryBuilder buildTermQuery(String conColumn, String conValue) {

        return QueryBuilders.termQuery(conColumn, conValue);//精确

    }

    /**
     * 模糊查询，匹配列conColumn，包含conValue的值，
     * 支持通配符匹配
     *
     * @param conColumn
     * @param conValue
     * @return
     */
    public static QueryBuilder buildLikeQuery(String conColumn, String conValue) {
        return QueryBuilders.wildcardQuery(conColumn, "*" + conValue + "*");
    }

    /**
     * 非查询
     * 构造  列 conColumn 值不为 conValue  查询条件
     * @param conColumn
     * @param conValue
     * @return
     */
    public static  QueryBuilder buildTermNotQuery(String conColumn, String conValue){
        QueryBuilder queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(conColumn, conValue));
        return  queryBuilder;
    }

    /**
     * 单个条件查询
     * 建立查询条件
     * 根据查询条件 QueryCondition 中指定 的查询类型进行查询，
     * @param queryCondtion
     * @return

     */

    public static  QueryBuilder buildQueryContion(QueryCondition queryCondtion){
        QueryTypeEnum queryType = queryCondtion.getQueryType();//查询类型
        String conColumn = queryCondtion.getConColumn();
        String conValue = queryCondtion.getConValue();
        String min = queryCondtion.getMin();
        String max = queryCondtion.getMax();
        QueryBuilder queryBuilder = null;
        switch (queryType) {
            case TERMQUERY:  queryBuilder = buildTermQuery( conColumn,  conValue);
                break;
            case LIKEQUERY:  queryBuilder = buildLikeQuery( conColumn,  conValue);
                break;
            case QUERYNOT:  queryBuilder = buildTermNotQuery( conColumn,  conValue);
                break;
            default:
                break;
        }
        return queryBuilder;

    }
    /**
     * must 条件绝对想等
     * 多条件查询
     * 建立多个查询条件
     * 根据查询条件 QueryCondition 中指定 的查询类型进行查询，
     * @param list
     * @return
     */

    public static  QueryBuilder buildQueryContion(List<QueryCondition> list){
        BoolQueryBuilder boolqueryBuilder = QueryBuilders.boolQuery();
        for(QueryCondition queryCondtion:list){
            QueryBuilder queryBuildert = buildQueryContion(queryCondtion);
            boolqueryBuilder.must(queryBuildert);
        }
        return boolqueryBuilder;
    }


    /**
     * should 相当于 in
     * 多条件查询
     * @param list
     * @return
     */
    public static  QueryBuilder buildQueryInContion(List<QueryCondition> list){
        BoolQueryBuilder boolqueryBuilder = QueryBuilders.boolQuery();
        for(QueryCondition queryCondtion:list){
            QueryBuilder queryBuildert = buildQueryContion(queryCondtion);
            boolqueryBuilder.should(queryBuildert);
        }
        return boolqueryBuilder;
    }
}